package cn.ejiajunxy.manager.provider;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.leo.baseui.mutiType.base.ItemViewProvider;

import cn.ejiajunxy.manager.R;
import cn.ejiajunxy.manager.adapter.ContentAdapter;
import cn.ejiajunxy.manager.model.OrderModel;


/**
 * Created by zhangguobao on 2018/3/14.
 */
public class OrderListBeanProvider
        extends ItemViewProvider<OrderModel.OrderListBean, OrderListBeanProvider.ViewHolder> {

    private boolean mIsSend;
    private AlertDialog mAlertDialog;
    private Callback mCallback;

    public OrderListBeanProvider(boolean isSend, Callback callback) {
        mIsSend = isSend;
        mCallback = callback;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.row_order_list_bean, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull final ViewHolder holder, @NonNull final OrderModel.OrderListBean orderListBean) {
        holder.tvOrder.setText(orderListBean.orderCode);
        holder.tvTime.setText("配送时间：   " + orderListBean.orderDate);
        if (orderListBean.address != null) {
            holder.tvAddress.setText("配送地址：   " + orderListBean.address.contactAddress);
        } else {
            holder.tvAddress.setText("配送地址：   ");
        }
        holder.llSend.setVisibility(mIsSend ? View.VISIBLE : View.GONE);
        holder.mContentAdapter.setData(orderListBean.shoppingCartList);
        holder.tvContant.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(holder.itemView.getContext());
                builder.setMessage("是否拨打我们的客服联系方式和？");
                builder.setPositiveButton("拨打", new DialogInterface.OnClickListener() {
                    @SuppressLint("MissingPermission")
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + orderListBean.account.MobilePhone));
                        holder.itemView.getContext().startActivity(intent);
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mAlertDialog.dismiss();
                    }
                });
                mAlertDialog = builder.create();
                mAlertDialog.show();
            }
        });
        holder.tvSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCallback.onSend(orderListBean.orderId);
            }
        });
    }

    public interface Callback {
        void onSend(String orderId);
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tvOrder;
        private RecyclerView rlContent;
        private LinearLayout llSend;
        private TextView tvTime;
        private TextView tvAddress;
        private TextView tvContant;
        private TextView tvSend;
        private ContentAdapter mContentAdapter;

        ViewHolder(View itemView) {
            super(itemView);
            tvOrder = (TextView) itemView.findViewById(R.id.tvOrder);
            rlContent = (RecyclerView) itemView.findViewById(R.id.rlContent);
            tvTime = (TextView) itemView.findViewById(R.id.tvTime);
            tvAddress = (TextView) itemView.findViewById(R.id.tvAddress);
            tvContant = (TextView) itemView.findViewById(R.id.tvContant);
            tvSend = (TextView) itemView.findViewById(R.id.tvSend);
            llSend = itemView.findViewById(R.id.llSend);
            rlContent.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
            mContentAdapter = new ContentAdapter();
            rlContent.setAdapter(mContentAdapter);
        }
    }
}